﻿using System.Configuration;
using NsbExtensions.EndpointMetadata;

namespace NsbExtensions.Configuration
{
    public class EndpointConfiguration : ConfigurationSection
    {
        [ConfigurationProperty("endpointId", IsRequired = true)]
        public string EndpointID
        {
            get { return (string) base["endpointId"]; }
            set { base["endpointId"] = value; }
        }

        [ConfigurationProperty("name", IsRequired = true)]
        public string Name
        {
            get { return (string) base["name"]; }
            set { base["name"] = value; }
        }


        [ConfigurationProperty("description", DefaultValue = "", IsRequired = false)]
        public string Description
        {
            get { return (string) base["description"]; }
            set { base["description"] = value; }
        }


        [ConfigurationProperty("messageBusAddress", IsRequired = true)]
        public string MessageBusAddress
        {
            get { return (string) base["messageBusAddress"]; }
            set { base["messageBusAddress"] = value; }
        }


        [ConfigurationProperty("techSupportEmailAddress", DefaultValue = " ", IsRequired = false)]
        public string SupportEmailAddress
        {
            get { return (string) base["techSupportEmailAddress"]; }
            set { base["techSupportEmailAddress"] = value; }
        }


        [ConfigurationProperty("techSupportTelephoneNumber", DefaultValue = "", IsRequired = false)]
        public string SupportTelephoneNumber
        {
            get { return (string) base["techSupportTelephoneNumber"]; }
            set { base["techSupportTelephoneNumber"] = value; }
        }


        [ConfigurationProperty("latitude", IsRequired = false)]
        public double EndpointLatitude
        {
            get { return (double) base["latitude"]; }
            set { base["latitude"] = value; }
        }


        [ConfigurationProperty("longitude", IsRequired = false)]
        public double EndpointLongitude
        {
            get { return (double) base["longitude"]; }
            set { base["longitude"] = value; }
        }

        [ConfigurationProperty("messageHandlerAssemblies", IsRequired = false)]
        public AssemblyConfigurationCollection MessageHandlerAssemblies
        {
            get { return (AssemblyConfigurationCollection) base["messageHandlerAssemblies"]; }
            set { base["messageHandlerAssemblies"] = value; }
        }

        public void CopyTo(EndpointDescription ep)
        {
            ep.EndpointID = EndpointID;
            ep.Name = Name;
            ep.Description = Description;
            ep.MessageBusAddress = MessageBusAddress;
            ep.SupportEmailAddress = SupportEmailAddress;
            ep.SupportTelephoneNumber = SupportTelephoneNumber;
            ep.EndpointLatitude = EndpointLatitude;
            ep.EndpointLongitude = EndpointLongitude;
        }

        public IEndpointDescription CreateEndpointDescription()
        {
            var desc = new EndpointDescription();
            CopyTo(desc);
            return desc;
        }

        public static EndpointConfiguration GetConfiguration()
        {
            return GetConfiguration("endpoint");
        }

        public static EndpointConfiguration GetConfiguration(string sectionName)
        {
            return ConfigurationManager.GetSection(sectionName) as EndpointConfiguration;
        }
    }
}